package logic

import (
	"context"

	"user/internal/svc"
	"user/pb/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetUserListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserListLogic {
	return &GetUserListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// GetUserList 获取用户列表
func (l *GetUserListLogic) GetUserList(in *user.UserListRequest) (*user.UserListResponse, error) {
	list, total, err := l.svcCtx.UserExtendModel.FindListByPage(
		l.ctx, in.Name, in.Mobile, in.Email, in.OrderBy, in.Status, in.LastId, in.Limit,
	)
	if err != nil {
		return nil, err
	}

	var data = make([]*user.UserInfo, 0)
	for _, item := range list {
		data = append(data, &user.UserInfo{
			Id:       item.Id,
			Password: "",
			Name:     item.Name,
			Mobile:   item.Mobile,
			Email:    item.Email,
			Avatar:   item.Avatar,
			Status:   item.Status,
			CreateAt: item.CreateAt.Unix(),
			UpdateAt: item.UpdateAt.Unix(),
		})
	}
	return &user.UserListResponse{Total: total, Data: data}, nil
}
